package com.nix.email;


import com.nix.web.service.user.UserServiceImpl;
import org.apache.commons.mail.EmailException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Create by zhangpe0312@qq.com on 2017/12/31.
 */
public abstract class ApacheMail {

    protected String propertiesPaht = "mail.properties";

    protected String hostName ;

    protected String name ;

    protected String password ;

    protected String fromAddress ;

    protected String fromName ;

    protected Properties properties = new Properties();

    /**
     * 导入默认配置文件mail.properties
     *
     * @throws IOException
     */
    public ApacheMail() throws IOException {
        InputStream in = null;
        try {
            in = ApacheMail.class.getClassLoader().getResourceAsStream(propertiesPaht);
            properties.load(in);
            this.hostName = (String) properties.get("hostName");
            this.name = (String) properties.get("name");
            this.password = (String) properties.get("password");
            this.fromAddress = (String) properties.get("fromAddress");
            this.fromName = (String) properties.get("fromName");
        }catch (FileNotFoundException e){
            System.out.println("文件未找到！" + e);
        }catch (IOException e){
            System.out.println(e);
        }finally {
            try {
                in.close();
            }catch (Exception e){
                System.out.println("关闭文件输入流出问题！" +e);
            }

        }

    }

    /**
     * 发送邮件
     *
     * @param toAddress
     * @param toName
     */
    abstract void sendEmail(String toAddress, String toName) throws EmailException;
}
